package cn.webestar.scms.microsvc.handler;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * @author zgs
 */
public class CommonMetaObjectHandler implements MetaObjectHandler {

    private static final String CREATED_TIME = "createTime";
    private static final String UPDATED_TIME = "updateTime";
    private static final String DELETED_TIME = "deleteTime";

    @Override
    public void insertFill(MetaObject metaObject) {

        String[] getterNames = metaObject.getGetterNames();

        if (ArrayUtil.contains(getterNames, CREATED_TIME)) {
            this.strictInsertFill(metaObject, CREATED_TIME, LocalDateTime.class, LocalDateTime.now());
        }
        if (ArrayUtil.contains(getterNames, UPDATED_TIME)) {
            this.strictInsertFill(metaObject, UPDATED_TIME, LocalDateTime.class, LocalDateTime.now());
        }
        if (ArrayUtil.contains(getterNames, DELETED_TIME)) {
            this.strictInsertFill(metaObject, DELETED_TIME, LocalDateTime.class, LocalDateTime.now());
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        String[] getterNames = metaObject.getGetterNames();
        if (ArrayUtil.contains(getterNames, UPDATED_TIME)) {
            this.strictUpdateFill(metaObject, UPDATED_TIME, LocalDateTime.class, LocalDateTime.now());
        }
    }

}
